/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Start.java
 *
 * Created on 9-nov-2009, 19.44.22
 */
package freimapgsoc;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author stefanopilla
 */
public class Start extends javax.swing.JFrame {

    /** Creates new form Start */
    public Start() {
        initComponents();
        CheckActiveDataSource check=new CheckActiveDataSource();
        if(check.check_olsr()){ //check DotDrawPlugin
            if(check.check_nameservice()){
             olsrdButton.setEnabled(true);
            }else{
            JOptionPane.showMessageDialog(this.getContentPane(),"Nameservice Plugin Must be enabled in the olsr.conf file for live data!\nOlsrd button will be enabled but the lat/lon position in the Map\nwill be DEFAULT_LAT/DEFAULT_LON (now in Rome)!");
              }
        }else{
            olsrdButton.setEnabled(false);
        }
    }

    /**
	 * This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 * @return 
	 */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        secondLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        openFileButton = new javax.swing.JButton();
        openExampleFile = new javax.swing.JButton();
        mySQLButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        olsrdButton = new javax.swing.JButton();
        batmanButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(Start.class);
        secondLabel.setFont(resourceMap.getFont("secondLabel.font")); // NOI18N
        secondLabel.setText(resourceMap.getString("secondLabel.text")); // NOI18N
        secondLabel.setName("secondLabel"); // NOI18N

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel1.border.titleFont"))); // NOI18N
        jPanel1.setFont(resourceMap.getFont("jPanel1.font")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        openFileButton.setFont(resourceMap.getFont("nameServiceButton.font")); // NOI18N
        openFileButton.setText(resourceMap.getString("openFileButton.text")); // NOI18N
        openFileButton.setName("openFileButton"); // NOI18N
        openFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openFileButtonActionPerformed(evt);
            }
        });

        openExampleFile.setFont(resourceMap.getFont("openExampleFile.font")); // NOI18N
        openExampleFile.setText(resourceMap.getString("openExampleFile.text")); // NOI18N
        openExampleFile.setName("openExampleFile"); // NOI18N
        openExampleFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openExampleFileActionPerformed(evt);
            }
        });

        mySQLButton.setFont(resourceMap.getFont("nameServiceButton.font")); // NOI18N
        mySQLButton.setText(resourceMap.getString("mySQLButton.text")); // NOI18N
        mySQLButton.setName("mySQLButton"); // NOI18N
        mySQLButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mySQLButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(openFileButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(openExampleFile, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(mySQLButton, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(7, Short.MAX_VALUE)
                .addComponent(mySQLButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(openFileButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(openExampleFile)
                .addGap(43, 43, 43))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel2.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel2.border.titleFont"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        olsrdButton.setFont(resourceMap.getFont("nameServiceButton.font")); // NOI18N
        olsrdButton.setText(resourceMap.getString("olsrdButton.text")); // NOI18N
        olsrdButton.setName("olsrdButton"); // NOI18N
        olsrdButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                olsrdButtonActionPerformed(evt);
            }
        });

        batmanButton.setFont(resourceMap.getFont("batmanButton.font")); // NOI18N
        batmanButton.setText(resourceMap.getString("batmanButton.text")); // NOI18N
        batmanButton.setEnabled(false);
        batmanButton.setName("batmanButton"); // NOI18N
        batmanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batmanButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(batmanButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(olsrdButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(olsrdButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(batmanButton)
                .addContainerGap(9, Short.MAX_VALUE))
        );

        exitButton.setIcon(resourceMap.getIcon("exitButton.icon")); // NOI18N
        exitButton.setText(resourceMap.getString("exitButton.text")); // NOI18N
        exitButton.setName("exitButton"); // NOI18N
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(95, 95, 95)
                        .addComponent(secondLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(secondLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(exitButton)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void olsrdButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_olsrdButtonActionPerformed
       // FreimapWizardPanel1 wizard=new FreimapWizardPanel1();
       // wizard.getComponent().setVisible(true);

        MySQLCredential mysqlcr=new MySQLCredential(new OlsrdDataSource());
       mysqlcr.setVisible(true);
    }//GEN-LAST:event_olsrdButtonActionPerformed

    private void openFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openFileButtonActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.addChoosableFileFilter(new xmlFileFilter());
        fc.addChoosableFileFilter(new jsFileFilter());
        //fc.addChoosableFileFilter(new FmpFileFilter());
        fc.setAcceptAllFileFilterUsed(false);
        int returnVal = fc.showOpenDialog(fc);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String extension = Utils.getExtension(file);
            if (extension.equals(Utils.xml)) {
//                                new Layer(new xmlDataSource().init(null));
            }
            if (extension.equals(Utils.js)) {
                new LatLonJsDataSource().init("file://" + file.getPath());
                this.dispose();
            }
            // FileMenu fm=new FileMenu();
            //fm.addRecentFile(file.getPath(), file.getName());
        } else if (returnVal == JFileChooser.CANCEL_OPTION) {
            System.out.println("Open command cancelled by user." + "\n");
        }        // TODO add your handling code here:
    }//GEN-LAST:event_openFileButtonActionPerformed

    private void openExampleFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openExampleFileActionPerformed
        new LatLonJsDataSource().init("http://www.stefanopilla.it/freimap/Datasource/latlon.js");
        this.dispose();

    }//GEN-LAST:event_openExampleFileActionPerformed

    private void mySQLButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mySQLButtonActionPerformed
        new CredentialRequired().setVisible(true);
            this.dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_mySQLButtonActionPerformed

    private void batmanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batmanButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_batmanButtonActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_exitButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Start().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton batmanButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton mySQLButton;
    private javax.swing.JButton olsrdButton;
    private javax.swing.JButton openExampleFile;
    private javax.swing.JButton openFileButton;
    private javax.swing.JLabel secondLabel;
    // End of variables declaration//GEN-END:variables
}
